/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.rantipin.GUI;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.LinkedHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
import ru.rantipin.utilities.ControlPhotoBoundListener;
import ru.rantipin.utilities.XMLHelper;

/**
 *
 * @author rantipin
 */
public class ControlPhotoPanel extends JPanel{ 
    
    public ControlPhotoPanel(ControlPhotoBoundListener listner) {
        this.listner = listner;
        adhesion = XMLHelper.getInstance().getAdhesionValues();
        initComponentsLayout();
    }
    
    public void setBlackWhiteBound(int value) {
        getRightBoundText().setText(Integer.toString(value));
    }
    
    public void setPercents(double black) {
        getBlackPercentText().setText(formatter.format(black));
        getWhitePercentText().setText(formatter.format(100.0 - black));
    }
    
    private void initComponentsLayout() {
        GridBagLayout gridbag = new GridBagLayout();
        setLayout(gridbag);
        
        gridbag.setConstraints(getCreateNewValues(), 
                new GridBagConstraints(
                    0, 0,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getCreateNewValues());
        
        gridbag.setConstraints(getPreserveValues(), 
                new GridBagConstraints(
                    0, 1,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getPreserveValues());
        
        gridbag.setConstraints(getRightBoundLabel(), 
                new GridBagConstraints(
                    0, 2,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 0),
                    0, 0
                ));
        add(getRightBoundLabel());
        
        gridbag.setConstraints(getRightBoundText(), 
                new GridBagConstraints(
                    1, 2,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 0),
                    0, 0
                ));
        add(getRightBoundText());
        
        gridbag.setConstraints(getButtonSaveDelete(), 
                new GridBagConstraints(
                    2, 2,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getButtonSaveDelete());
        
        gridbag.setConstraints(getPercent(), 
                new GridBagConstraints(
                    0, 3,
                    GridBagConstraints.REMAINDER, 1,
                    1.0, 0.0,
                    GridBagConstraints.PAGE_START, GridBagConstraints.HORIZONTAL,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getPercent());
    }
    
    private JCheckBox getCreateNewValues() {
        if (createNewValues == null) {
            createNewValues = new JCheckBox("Manual control of right bound", false);
            createNewValues.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    boolean selected = createNewValues.isSelected();
                    listner.manualControl(selected);
                    getPreserveValues().setVisible(!selected);
                    getRightBoundText().setEditable(selected);
                    getButtonSaveDelete().setText(selected? "Save": "Delete");
                    getRightBoundText().setText(selected ? Double.toString(100.0): "");
                }
            });
        }
        
        return createNewValues;
    }
    
    private JComboBox getPreserveValues() {
        if (preserveValues == null) {
            preserveValues = new JComboBox(getAdhesionName());
            preserveValues.setEditable(false);
            preserveValues.setPreferredSize(new Dimension(0, 25));
            preserveValues.setMaximumRowCount(4);
            //preserveValues.setSelectedIndex(0);
            preserveValues.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ;
                }
            });
        }
        
        return preserveValues;
    }
    
    private String[] getAdhesionName() {
        String[] names = new String[adhesion.size()];
        return adhesion.keySet().toArray(names);
    }
    
    private void setBlackWhiteBound(String name) {
        //adhesion.
        //listner.settingBlackWhiteBound();
    }
    
    private JLabel getRightBoundLabel() {
        if (rightBoundLabel == null) {
            rightBoundLabel = new JLabel("Right bound:");
            rightBoundLabel.setLabelFor(getRightBoundText());
        }
        
        return rightBoundLabel;
    }
    
    private JFormattedTextField getRightBoundText() {
        if (rightBoundText == null) {
            MaskFormatter format = null;
            try {
                format = new MaskFormatter("###");
            } catch(ParseException ex) {
                String message = "Can't setup mask";
                Logger.getLogger(ControlPhotoPanel.class.getName()).log(Level.SEVERE, message, ex);
            }
            
            rightBoundText = new JFormattedTextField(format);
            rightBoundText.setColumns(5);
            rightBoundText.setEditable(false);
            rightBoundText.addKeyListener(new KeyAdapter() {
                @Override
                public void keyReleased(KeyEvent e) {
                    super.keyReleased(e);
                    String str = rightBoundText.getText().trim();
                    if (!"".equals(str)) {
                        int value = Integer.parseInt(str);
                        if(!str.equals(String.valueOf(value))) {
                            rightBoundText.setText(String.valueOf(value));
                        }
                        if (value > 100) {
                            rightBoundText.setText("100");
                        }
                    } else {
                        rightBoundText.setText("0");
                    }
                    
                    listner.settingBlackWhiteBound(Integer.parseInt(rightBoundText.getText().trim()));
                }
            }); 
        }
        
        return rightBoundText;
    }
    
    private JButton getButtonSaveDelete() {
        if (buttonSaveDelete == null) {
            buttonSaveDelete = new JButton("Delete");
            buttonSaveDelete.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (getCreateNewValues().isSelected()) {
                        //save
                    } else {
                        //delete
                        getPreserveValues().removeItemAt(getPreserveValues().getSelectedIndex());
                    }
                }
            });
        }
        
        return buttonSaveDelete;
    }
    
    private JPanel getPercent() {
        if (percent == null) {
            percent = new JPanel();
            percent.setBorder(BorderFactory.createTitledBorder("Components percent"));
            
            GridBagLayout gridbag = new GridBagLayout();
            percent.setLayout(gridbag);
            
            gridbag.setConstraints(getBlackPercentLable(), 
                new GridBagConstraints(
                    0, 0,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 10, 0),
                    0, 0
                ));
            percent.add(getBlackPercentLable());
            
            gridbag.setConstraints(getBlackPercentText(), 
                new GridBagConstraints(
                    1, 0,
                    1, 1,
                    1.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 10, 0),
                    0, 0
                ));
            percent.add(getBlackPercentText());
            
            gridbag.setConstraints(getWhitePercentLable(), 
                new GridBagConstraints(
                    2, 0,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 10, 10, 0),
                    0, 0
                ));
            percent.add(getWhitePercentLable());
            
            gridbag.setConstraints(getWhitePercentText(), 
                new GridBagConstraints(
                    3, 0,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 10, 10, 10),
                    0, 0
                ));
            percent.add(getWhitePercentText());
        }
        
        return percent;
    }
    
    private JLabel getBlackPercentLable() {
        if (blackPercent == null) {
            blackPercent = new JLabel("Black:");
            blackPercent.setLabelFor(getBlackPercentText());
        }
        
        return blackPercent;
    }
    
    private JTextField getBlackPercentText() {
        if (blackPercentText == null) {
            blackPercentText = new JTextField(5);
            blackPercentText.setEditable(false);
            blackPercentText.setBackground(this.getBackground());
        }
        
        return blackPercentText;
    }
    
    private JLabel getWhitePercentLable() {
        if (whitePercent == null) {
            whitePercent = new JLabel("White:");
            whitePercent.setLabelFor(getWhitePercentText());
        }
        
        return whitePercent;
    }
    
    private JTextField getWhitePercentText() {
        if (whitePercentText == null) {
            whitePercentText = new JTextField(5);
            whitePercentText.setEditable(false);
            whitePercentText.setBackground(this.getBackground());
        }
        
        return whitePercentText;
    }
    
    private LinkedHashMap<String, Integer> adhesion = null;
    
    private DecimalFormat formatter = new DecimalFormat("0.00") ;
    private ControlPhotoBoundListener listner = null;
    
    private JCheckBox createNewValues = null;
    private JComboBox preserveValues = null;
    private JLabel rightBoundLabel = null;
    private JFormattedTextField rightBoundText = null;
    private JButton buttonSaveDelete = null;
    private JPanel percent = null;
    private JLabel blackPercent = null;
    private JTextField blackPercentText = null;
    private JLabel whitePercent = null;
    private JTextField whitePercentText = null;
}
